<?php
/*
 * 手机客户端 展品明细
 * 2013-12-04
 */
 
include "inc/public_header.php";

include "func/user.php";				//用户信息处理函数
include "func/credit.php";				//用户竞拍信息数据

//小图URL根路径
$S_IMG_BASE_URL = "http://www.chinalaohu.com/modules/miniature.php?w=620&h=316&ew=620&p=/uploads/1000*1000/";

$S_IMG_BASE_zjl_URL = "http://www.chinalaohu.com/modules/miniature.php?w=620&h=316&ew=620&p=/uploads/zjl/";
//大图URL根路径
$IMG_BASE_URL = "http://img1.chinalaohu.com/uploads/1000*1000/";

$IMG_BASE_zjl_URL = "http://img1.chinalaohu.com/uploads/zjl/";

$aid 		= $_REQUEST["aID"];		//展品主键

$reJson = array(); //构建返回值

$isLead		= 0;	//是否领先
$isProxy	= 0;	//是否使用了代理
$proxyPrice	= 0;	//代理价
$mobile		= "";	//当前用户手机号
$credit		= 0;	//用户信用额度

$userjson = array(); //用户信息

//获取用户信息
$uInfoArr = user_getInfoFromUUID();
if($uInfoArr){
	$uid = $uInfoArr["uid"]; 		//用户信息主键
	$mobile = $uInfoArr["mobile"];	//用户手机号
	//判断是否领先
	$sql = "select uid,is_bid from auction_userlastbid where aid='".$aid."' and uid='".$uid."'";
	$db->query($sql);
	if($db->next_record()){
		$isLead = $db->f("is_bid");
	}
	//是否采用代理
	$sql = "select bidproxy from auction_userbidproxy where aid='".$aid."' and uid='".$uid."'";
	$db->query($sql);
	if($db->next_record()){
		$isProxy = 1;
		$proxyPrice = $db->f("bidproxy");
	}
	$name = $uInfoArr["realname"]; //先采用真实姓名
	if(!$name){
		$name = $uInfoArr["mobile"]; //无奈不知道你的芳名，只能使用手机号
	}
	$userjson = array(
		 "isLogin"=>"1"
		,"name"=>$name
		,"urank"=>urankfunc($uInfoArr["urank"])
		,"state"=>$uInfoArr["ustatus"]
	);
}else{
	$userjson = array(
		"isLogin"=>"0"
	);
}

//获取展品信息
$sql = "select 
				a.aid
				,a.acode
				,a.auction
				,a.servicefee
				,b.astid
				,a.bids
				,a.zp_img_name
				,if(a.lastprice = 0,a.startprice,a.lastprice) as showprice 
				,date_format(b.asprevtime,'%Y-%m-%d %H:%i:%s') as asprevtime
				,date_format(b.asprevclosetime,'%Y-%m-%d %H:%i:%s') as asprevclosetime
				,date_format(b.asopentime,'%Y-%m-%d %H:%i:%s') as asopentime
				,date_format(b.asclosetime,'%Y-%m-%d %H:%i:%s') as asclosetime
				,if(b.asclosetime<=now(),2,if(b.asopentime<=now(),1,0)) as auctiontype
		from 
				auction a 
				inner join auction_special b on a.asid=b.asid
		where a.aid='".$aid."'";

$db->query($sql);
if($db->next_record()){
	//拍卖出价记录
	$sql = "select u.mobile as nick,a.bid,date_format(a.bid_dt,'%Y-%m-%d %H:%i:%s') as bid_dt,a.is_bid from auction_userbid a left outer join user_base u on a.uid=u.uid where a.aid='".$aid."' order by a.aubid desc limit 10";
	$db2->query($sql);

	$reJson = array(
		 "success"=>"1"
		,"serverTime"=>date('Y-m-d H:i:s')
	);
	$history = array(); //竞拍历史
	while($db2->next_record()){
		$mine = 0;
		if($mobile && $mobile==$db2->f("nick")){
			$mine = 1;
		}
		$nickname = $db2->f("nick");
		$nickname = substr($nickname,0,3)."*****".substr($nickname,strlen($nickname)-3,3);
		$history[] = array(
			 "name"=>$nickname
			,"time"=>$db2->f("bid_dt")
			,"price"=>$db2->f("bid")
			,"mine"=>(string)$mine
			,"state"=>$db2->f("is_bid")
		);
	}
	
	//固定六张小图URL
	$imgUrl = array();
	//固定六张大图URL
	$imgOriginal = array();
	if($db->f("astid")==1){
	

		for($i=1;$i<7;$i++){
			$imgUrl[] = $S_IMG_BASE_URL.$db->f("acode")."f".$i.".jpg";
			$imgOriginal[] = $IMG_BASE_URL.$db->f("acode")."f".$i.".jpg";
		}
	}else{

		$imgUrl[] = $S_IMG_BASE_zjl_URL.$db->f("zp_img_name");
		$imgOriginal[] = $IMG_BASE_zjl_URL.$db->f("zp_img_name");
	
	}

	//信用额度
	if($uid){
		$credit = credit_creditLimit($uid);
		if($isLead){
				//如果目前领先，在竞拍该商品时，需要把出价加回来，好让用户可以更新价格
				//如果不加回来，用户再更新该商品的出价时，会从信用额度中再次扣除保证金，可能会导致
				//额度不足
			if($isProxy){
				$credit = $credit+$proxyPrice;
			}else{
				$credit = $credit+$db->f("showprice");
			}
		}
	}else{
		$credit = 0;
	}
	
	//是否有人竞价
	if($db->f("bids")==0){
		$isOrigin = 1;
	}else{
		$isOrigin = 0;
	}
	//展品信息
	$auctionjson = array(
		"aID"=>(string)$db->f("aid")
		,"imgUrl"=>$imgUrl
		,"bigImgUrl"=>$imgOriginal
		,"name"=>$db->f("auction")
		,"auctionCode"=>(string)$db->f("acode")
		,"price"=>(string)$db->f("showprice")
		,"credit"=>(string)round($credit,2)
		,"servicePrice"=>(string)$db->f("servicefee")
		,"rank"=>(string)addpfunc($db->f("showprice"))
		,"isOrigin"=>(string)$isOrigin
		,"isLead"=>(string)$isLead
		,"isProxy"=>(string)$isProxy
		,"proxyPrice"=>(string)$proxyPrice
		,"auctionType"=>(string)$db->f("auctiontype")
		,"dealTime"=>(string)$db->f("asclosetime")
		,"lastTime"=>(string)$db->f("asclosetime")
		,"preSTime"=>(string)$db->f("asprevtime")
		,"preETime"=>(string)$db->f("asprevclosetime")
	);
	
	$reJson["user"] = $userjson;
	$reJson["history"] = $history;
	$reJson["auction"] = $auctionjson;
	
	
}else{
	$reJson = array("success"=>"0","msg"=>"没有获取到展品信息");
}
echo json_encode($reJson);
die;



/**
 * 加价幅度
 */
function addpfunc($jg){
	if ($jg<=50){
		return 1;
	}else if(jg>50 && jg<=100){
		return 2;
	}else if(jg>100 && jg<=500){
		return 5;
	}else if(jg>500 && jg<=1000){
		return 10;
	}else if(jg>1000 && jg<=2000){
		return 20;
	}else if(jg>2000 && jg<=5000){
		return 50;
	}else if(jg>5000 && jg<=10000){
		return 100;
	}else if(jg>10000 && jg<=20000){
		return 200;
	}else if(jg>20000 && jg<=50000){
		return 500;
	}else if(jg>50000 && jg<=200000){
		return 1000;
	}else {
		return 2000;
	}
}

/**
 * 母鸡
 */
function urankfunc($urank){
	switch($urank){
		case 0:
			return '未注册会员';
			break;
		case 1:
			return '注册会员';
			break;
		case 2:
			return '一钻会员';
			break;
		case 3:
			return '二钻会员';
			break;
		case 4:
			return '三钻会员';
			break;
		case 5:
			return '四钻会员';
			break;
		case 6:
			return '五钻会员';
			break;
		case 7:
			return '一皇冠';
			break;
		case 8:
			return '二皇冠';
			break;
	}
}
 ?>